package com.lmh.graphdisease.controller;

import com.lmh.graphdisease.entity.DiseaseEntity;
import com.lmh.graphdisease.entity.FoodEntity;
import com.lmh.graphdisease.service.DiseaseService;
import com.lmh.graphdisease.service.OtherService;
import com.lmh.graphdisease.utils.ResultBean;
import com.lmh.graphdisease.vo.OtherEntityVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lmh
 * @desc
 * @date 2023/4/10
 */
@RestController
@RequestMapping("/admin")
public class AdminController {


    @Autowired
    private DiseaseService diseaseService;

    @Autowired
    private OtherService otherService;

    /**
     *@author lmh
     *@date 2023/4/10
     *@desc 后台管理，分页查询疾病实体
     */
    @RequestMapping(path = "/diseasePage",method = RequestMethod.POST)
    public ResultBean getDiseasePage(@RequestParam(required = true) Integer page, @RequestParam(required = true) Integer size){
        int curBegin = (page - 1) * size;
        List<DiseaseEntity> someDisease = diseaseService.getPageDisease(curBegin,size);
        if(someDisease == null || someDisease.size() == 0){
            return ResultBean.fail(101,"meizhaodao");
        }else {
            return ResultBean.success(someDisease);
        }
    }

    @RequestMapping(path = "/diseaseNum",method = RequestMethod.POST)
    public ResultBean getDiseaseNum(){
        return ResultBean.success(diseaseService.getDiseaseNum());
    }


    @RequestMapping(path = "/otherPage",method = RequestMethod.POST)
    public ResultBean getFoodPage(@RequestParam(required = true) String identity,@RequestParam(required = true) Integer page, @RequestParam(required = true) Integer size){
        int curBegin = (page - 1) * size;
        List<OtherEntityVO> sommOther = otherService.getPageOther(identity,curBegin,size);
        if(sommOther == null || sommOther.size() == 0){
            return ResultBean.fail(101,"meizhaodao");
        }else {
            return ResultBean.success(sommOther);
        }
    }

    @RequestMapping(path = "/otherNum",method = RequestMethod.POST)
    public ResultBean getOtherNum(@RequestParam(required = true) String identity){
        return ResultBean.success(otherService.getOtherNum(identity));
    }


    /**
     *@author lmh
     *@date 2023/4/15
     *@desc 其他节点只需要插入name就行（只有这一个属性）
     */
    @RequestMapping(path = "/otherAdd",method = RequestMethod.POST)
    public ResultBean addOther(@RequestParam(required = true) String identity,@RequestParam(required = true) String name){
        if(name == null || name.equals("")){
            return ResultBean.fail(404,"没接收到数据");
        }
        boolean b = otherService.addOtherLabel(identity,name);
        if(b){
            return ResultBean.success(b);
        }else {
            return ResultBean.fail(405,"插入失败");
        }
    }

    @RequestMapping(path = "/otherDelete1",method = RequestMethod.POST)
    public ResultBean deleteOther(@RequestParam(required = true) String identity,@RequestParam(required = true) String name){
        boolean b = otherService.deleteOtherNoRelationship(identity,name);
        if(b){
            return ResultBean.success(b);
        }else {
            return ResultBean.fail(405,"删除失败");
        }
    }



}
